<?php

namespace App\Services\SpiderContent;

use Carbon\Carbon;
use App\Models\User;
use App\Models\News;
use App\Jobs\RewardFirstParentJob;
use Illuminate\Support\Facades\Log;
use Rainsens\Dinglog\Facades\Dinglog;
use Illuminate\Database\Eloquent\Collection;
use Sunra\PhpSimple\HtmlDomParser;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

use DOMDocument;


class SpiderHuxiaoContent
{

    public function execute($baseUrl)
    {
    	ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');

    	
    	//$url = "https://www.baidu.com";
    	$html = file_get_contents($baseUrl);
    	//$html = '<a href="aaaa"><div class="content-item"><div>测试</div></div></a>';
    	$dom = new DOMDocument();
    	

    	libxml_use_internal_errors(true);
    	//$html = htmlspecialchars($html);
    	
		
        $dom->loadHTML('<?xml encoding="UTF-8">' . $html);
      
        $elements = $dom->getElementsByTagName('div');

     	//$html = HtmlDomParser::str_get_html($html);

     	//dd($elements->nodeValue);

     	$data = [];
     	$i = 0;

     	$briefMatch = "/\"brief_id\":\d+,/";
        foreach($elements as $key=>$val){
        	$url = "";

    		if (strpos($val->getAttribute('class'), 'content-item') !== false) {
    	 		$content =   $dom->saveHTML($val);
    	 		$brief = preg_match($briefMatch,$content,$match);
    	 		if(!empty($match)){
    	 			preg_match('/\d+/',$match[0],$briefIds);

    	 		}
    	 		if(!empty($briefIds)){

    	 			$briefId= $briefIds[0];
    	 			$url = "https://www.huxiu.com/brief/{$briefIds[0]}.html";
    	 		}
    	 		
	        	//$data['title'] = $val->childNodes[0]->textContent;
	        		
	        	$contentHtml = file_get_contents($url);
	        	
	        	$contentDom = new DOMDocument();

	        	$contentDom->loadHTML('<?xml encoding="UTF-8">' . $contentHtml);
	        	$Contentelements = $contentDom->getElementsByTagName('h4');


	        	
        		foreach($Contentelements as $childNode){
        			
        			if (strpos($childNode->getAttribute('class'), 'text-sm-title') !== false) {
        				$data[$i]['title_cn'] = $childNode->textContent;
        				$data[$i]['url'] = $url;
        				$res = $this->djNodes($childNode,['content'=>"","image"=>""]);
        				$data[$i]['content_cn']  = $res['content'];
        				//$data[$i]['images']  = $res['image'];
        				if($res['image']){

        					$imageContent = file_get_contents($res['image']);
 							$newFileName = "/images/".Str::random(40) . '.jpg'; // 假设是JPG格式
							Storage::disk('oss')->put($newFileName, $imageContent);
							 
							// 你现在可以通过存储的路径访问图片
							$res['image'] = Storage::disk('oss')->path($newFileName);
							
        				}
        				$data[$i]['images']  = $res['image'];
        				$data[$i]['source'] = "huxiu";
        				$data[$i]['created_at'] = Carbon::now();

        				$i++;
        				//dd($res);
        				//$data[$i]['content_cn'] = $res;

        			}	
        		}
        		break;
	        	
	        	
	        	/*$data['content'] = $contentDom->saveHTML($Contentelement);
	        	break;*/
    	 		//echo $dom->saveHTML($val);
    	 		//echo iconv("UTF-8", "GB18030//TRANSLIT", $dom->saveXML($val) );
    	 }

        	
        	  
		}


		DB::table('news')->insert($data);
		//dd($data);
     	 
    	//dd($elements);

     	//dd(retrieve_url_contents($url));
     	//$html = file_get_contents('https://www.huxiu.com/club/1000.html?object_type=51&object_id=1');
     	//dd($html);
 
		
    }


    public function djNodes($nodes,$res)
    {
    	if($nodes->nextSibling && $nodes->nextSibling->nodeName!="h4"){

    		$res['content'] = $res['content'].$nodes->nextSibling->textContent;


    		if($nodes->nextSibling->childNodes){

    			foreach($nodes->nextSibling->childNodes as $nn){
    				if($nn->nodeName=="img"){
    					$res['image'] = $nn->getAttribute('src');
    				}
    			}
    		}
    		
    		if(isset($nodes->nextSibling->nextSibling)  && $nodes->nextSibling->nextSibling->nodeName != "h4"){

    			 $res = $this->djNodes($nodes->nextSibling,$res);
    		}
    	}

    	return  $res;
    	//dd($nodes->nextSibling->nextSibling);
    	

    }

	function getUrlSize($url) {
	    // 初始化cURL

	   ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');

	    $ch = curl_init($url);
	     dd($ch);
	    // 设置cURL选项
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($ch, CURLOPT_HEADER, 1);
	    curl_setopt($ch, CURLOPT_NOBODY, 1);
	    
	    // 执行cURL请求
	    $output = curl_exec($ch);
	    // 获取内容长度
	    $size = 0;
	    if (preg_match('/Content-Length: (\d+)/', $output, $matches)) {

	        $size = (int)$matches[1];
	    }
	    
	    // 关闭cURL
	    curl_close($ch);
	    
	    return $size;
	}

	public function __invoke()
	{
	
		//Dinglog::text("========== refresh seckill product num start ......");

		$baseUrl1 = "https://www.huxiu.com/club/1000.html?object_type=51&object_id=1";
		$baseUrl2 = "https://www.huxiu.com/club/1000.html?object_type=51&object_id=2";
		$this->execute($baseUrl1);
		$this->execute($baseUrl2);

		//Dinglog::text("========== refresh seckill product num end ......");
	}
}
